package com.um.swing.component;

import cn.hutool.core.util.StrUtil;
import com.um.swing.bean.ImageItem;
import com.um.swing.dialog.JPostionSettingDialog;
import com.um.swing.common.inc.Config;
import com.um.swing.common.inc.Constant;
import com.um.swing.ui.IBottomPointLabelUI;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Function;

/**
 * 自定义组件：底部XY显示组件
 * 袁泉
 * 2022年04月26日18:57:06
 */
public class JBottomPointInfoPanel extends JPanel {
    private ImageItem mImageItem;

    private static final Color curLabelColor = new Color(245, 84, 0);
    private static final Color generalLabelColor = Constant.MAIN_FR_COLOR;

    private JLabel jLabelP1, jLabelP2, jLabelP3, jLabelP4;
    private MouseAdapter mLabelMouseAdapter;//label点击事件
    private JBottomPointInfoPanel instance = null;
    //回调事件
    private Function<Integer, Object> clickEvent;//点击事件

    public void setClickEvent(Function<Integer, Object> clickEvent) {
        this.clickEvent = clickEvent;
    }

    public JBottomPointInfoPanel() {
        instance = this;
        jLabelP1 = createBottomStatusJLabel();
        jLabelP2 = createBottomStatusJLabel();
        jLabelP3 = createBottomStatusJLabel();
        jLabelP4 = createBottomStatusJLabel();
        this.setOpaque(false);
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.add(jLabelP1);
        this.add(jLabelP2);
        this.add(jLabelP3);
        this.add(jLabelP4);

        mLabelMouseAdapter = initMouseAdapter();
        jLabelP1.addMouseListener(mLabelMouseAdapter);
        jLabelP2.addMouseListener(mLabelMouseAdapter);
        jLabelP3.addMouseListener(mLabelMouseAdapter);
        jLabelP4.addMouseListener(mLabelMouseAdapter);

//        this.addMouseListener();
    }


    public void refreshBottomPointInfo(ImageItem item) {
        mImageItem = item;
        Point p1 = item.getPointMapItem(ImageItem.P1);
        Point p2 = item.getPointMapItem(ImageItem.P2);
        Point p3 = item.getPointMapItem(ImageItem.P3);
        Point p4 = item.getPointMapItem(ImageItem.P4);

        String x[] = new String[]{"-1", "-1", "-1", "-1"};
        String y[] = new String[]{"-1", "-1", "-1", "-1"};

        if (p1 != null) {
            x[0] = String.valueOf(p1.x);
            y[0] = String.valueOf(p1.y);
        }
        if (p2 != null) {
            x[1] = String.valueOf(p2.x);
            y[1] = String.valueOf(p2.y);
        }
        if (p3 != null) {
            x[2] = String.valueOf(p3.x);
            y[2] = String.valueOf(p3.y);
        }
        if (p4 != null) {
            x[3] = String.valueOf(p4.x);
            y[3] = String.valueOf(p4.y);
        }
        jLabelP1.setText(StrUtil.format("坐标1：{},{}", x[0], y[0]));
        jLabelP2.setText(StrUtil.format("坐标2：{},{}", x[1], y[1]));
        jLabelP3.setText(StrUtil.format("坐标3：{},{}", x[2], y[2]));
        jLabelP4.setText(StrUtil.format("坐标4：{},{}", x[3], y[3]));

        setCurIndex(item.getPointIndex());
    }

    private MouseAdapter initMouseAdapter() {
        return new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
                if (e.getButton() == MouseEvent.BUTTON3) {
                    JPostionSettingDialog.getInstance().setVisible(true);
                } else {
                    JLabel label = (JLabel) e.getComponent();
                    String text = label.getText();
                    if (StrUtil.isBlank(text)) {
                        return;
                    }
                    int index = Integer.valueOf(text.substring(2, 3));
                    setCurIndex(index);
                }
            }
        };
    }

    private void setCurIndex(int index) {
        jLabelP1.setForeground(generalLabelColor);
        jLabelP2.setForeground(generalLabelColor);
        jLabelP3.setForeground(generalLabelColor);
        jLabelP4.setForeground(generalLabelColor);
        switch (index) {
            case 1:
                jLabelP1.setForeground(curLabelColor);
                break;
            case 2:
                jLabelP2.setForeground(curLabelColor);
                break;
            case 3:
                jLabelP3.setForeground(curLabelColor);
                break;
            case 4:
                jLabelP4.setForeground(curLabelColor);
                break;
        }
        if (mImageItem.getPointIndex() != index) {
            mImageItem.setPointIndex(index);
        }
        if (clickEvent != null) {
            clickEvent.apply(index);
        }
    }

    private JLabel createBottomStatusJLabel() {
        JLabel label = new JLabel();
        label.setOpaque(false);
        label.setBorder(new EmptyBorder(0, 10, 0, 0));
        label.setForeground(generalLabelColor);
        label.setUI(new IBottomPointLabelUI());
        label.setFont(Constant.FONT_BASE_12);
        return label;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }
}
